/* @(#)FseCommunityMemberParticipationSummary.java
** 
** Copyright (C) 2012 by Steven D. Stamps
**
**             Trademarks & Copyrights
** Flywheel Management Science(TM), Flywheel Management Model(TM),
** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
** of Steven D. Stamps and may only be used freely for the purpose of
** identifying the unforked version of this software.  Subsequent forks
** may not use these trademarks.  All other rights are reserved.
**
** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
** are also exclusive trademarks of Steven D. Stamps.  These may be used
** freely within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** gConGUI (game Controller Graphical User Interface) is an exclusive
** trademark of Steven D. Stamps.  This trademark may be used freely
** within the unforked FlywheelMS application and documentation.
** All other rights are reserved.
**
** Trademark information is available at
** <http://www.flywheelms.com/trademarks>
**
** Flywheel Management Science(TM) is a copyrighted body of management
** metaphors, governance processes, and leadership techniques that is
** owned by Steven D. Stamps.  These copyrighted materials may be freely
** used, without alteration, by the community (users and developers)
** surrounding this GPL3-licensed software.  Additional copyright
** information is available at <http://www.flywheelms.org/copyrights>
**
**              GPL3 Software License
** This program is free software: you can use it, redistribute it and/or
** modify it under the terms of the GNU General Public License, version 3,
** as published by the Free Software Foundation. This program is distributed
** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
** PURPOSE.  See the GNU General Public License for more details. You should
** have received a copy of the GNU General Public License, in a file named
** COPYING, along with this program.  If you cannot find your copy, see
** <http://www.gnu.org/licenses/gpl-3.0.html>.
*/

package com.flywheelms.library.fse.model;

import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.flywheelms.library.fmm.serialization.FmmSerialization;
import com.flywheelms.library.fmm.serialization.FseDocumentSerialization;

public class FseCommunityMemberParticipationSummary implements FmmSerialization {
	
	private ArrayList<FseCommunityMemberParticipation> communityMemberParticipationList = new ArrayList<FseCommunityMemberParticipation>();
	
	public FseCommunityMemberParticipationSummary() {
	}
	
	public FseCommunityMemberParticipationSummary(JSONObject aJsonObject) {
		JSONArray theJsonArray = new JSONArray();
		try {
			theJsonArray = aJsonObject.getJSONArray(FseDocumentSerialization.key__COMMUNITY_MEMBER_PARTICIPATION_LIST);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0; i < theJsonArray.length(); i++) {
			JSONObject theJsonObject;
			try {
				theJsonObject = theJsonArray.getJSONObject(i);
				this.communityMemberParticipationList.add(new FseCommunityMemberParticipation(theJsonObject));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public ArrayList<FseCommunityMemberParticipation> getCommunityMemberParticipationList() {
		if(this.communityMemberParticipationList.size() == 0) {
			generateTestData();
		}
		return this.communityMemberParticipationList;
	}

	private void generateTestData() {
		this.communityMemberParticipationList.add(new FseCommunityMemberParticipation(
				"Adam Dye",
				new Date(),
				new Date(),
				5,
				18,
				3,
				7,
				1,
				7,
				14,
				1,
				8,
				2) );
		this.communityMemberParticipationList.add(new FseCommunityMemberParticipation(
				"Steve Stamps",
				new Date(),
				new Date(),
				15,
				23,
				7,
				11,
				3,
				11,
				18,
				2,
				14,
				4) );
	}

	@Override
	public String getSerialized() {
		return getJsonObject().toString();
	}

	@Override
	public JSONObject getJsonObject() {
		JSONObject theJsonObject = new JSONObject();
		try {
			theJsonObject.put(FseDocumentSerialization.key__COMMUNITY_MEMBER_PARTICIPATION_LIST, getParticipationList());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return theJsonObject;
	}

	private JSONArray getParticipationList() {
		JSONArray theJsonArray = new JSONArray();
		for(FseCommunityMemberParticipation theCommunityMemberParticipation : this.communityMemberParticipationList) {
			theJsonArray.put(theCommunityMemberParticipation.getJsonObject());
		}
		return theJsonArray;
	}

}
